﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Crucible.Models;
using WebMatrix.WebData;
using Crucible.Filters;
using Crucible.DocSearch;

namespace Crucible.Controllers
{

    public class NodeController : Controller
    {
        private ArgumentConnection db = new ArgumentConnection();

        //
        // GET: /Assertion/

        public ActionResult Index()
        {
            //return (View(from node in db.Nodes.Where()
            //                 join ed in db.NodeLinks on node equals ed.Child into 
            //                 where ed)
            var query =
                db.Nodes.Where(n => !db.NodeLinks.Any(link => link.Child == n))
                .OrderByDescending(n => n.DateAdded)
                .Take(20);

            return View(query.ToList());
        }

        //
        // GET: /Assertion/Details/5

        public ActionResult Details(int id = 0)
        {
            Node assertion = db.Nodes.Include(a => a.AddedBy).SingleOrDefault(a => a.Id == id);
            if (assertion == null)
            {
                return HttpNotFound();
            }
            return View(assertion);
        }

        //
        // POST: /Assertion/Details
        [HttpPost]
        public ActionResult Details(FormCollection collection)
        {
            int id = 0;
            int.TryParse(collection.Get("id"), out id);

            Node assertion = db.Nodes.Include(a => a.AddedBy).SingleOrDefault(a => a.Id == id);
            if (assertion == null)
            {
                return HttpNotFound();
            }
            return Json(assertion);
        }

        //
        // GET: /Assertion/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Assertion/Create

        [HttpPost]
        [Authorize]
        public ActionResult Create(Node assertion)
        {
            assertion.AddedBy_UserId = WebSecurity.CurrentUserId;

            if (ModelState.IsValid)
            {
                db.Nodes.Add(assertion);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(assertion);
        }


        //
        // POST: /Assertion/SaveDraft
        

        //
        // GET: /Assertion/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Node assertion = db.Nodes.Find(id);
            if (assertion == null)
            {
                return HttpNotFound();
            }
            return View(assertion);
        }

        //
        // POST: /Assertion/Delete/5

        [Authorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Node assertion = db.Nodes.Find(id);
            if (assertion.Published)
            {
                throw new HttpException(403, "You can't delete an assertion once it has been published.");
            }

            if (assertion.AddedBy_UserId != WebSecurity.CurrentUserId)
            {
                throw new HttpException(403, "You can't delete somebody else's assertion.");
            }

            db.Nodes.Remove(assertion);
            db.SaveChanges();
            IndexManager.DeleteAssertion(assertion);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}